package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.AppFile;
import com.wzcl.app.dao.repository.AppFileRepository;
import com.wzcl.app.model.dto.FileDto;
import com.wzcl.app.service.AppFileService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Log4j2
@Service
public class AppFileServiceImpl implements AppFileService {
    @Autowired
    private AppFileRepository repository;


    @Override
    public AppFile addFile(FileDto fileDto) {
        AppFile file = getFileByKey(fileDto.getFileKey());
        if (file != null) {
            return file;
        }
        file = new AppFile();
        BeanUtils.copyProperties(fileDto, file);
        return repository.save(file);
    }

    @Override
    public AppFile getFileById(Integer id) {
        Optional<AppFile> optional = repository.findById(id);
        return optional.orElse(null);
    }

    @Override
    public AppFile getFileByKey(String key) {
        return repository.findByFileKey(key);
    }

}
